import zmq
import pickle

def init_socket(bind=True, port=5555, host="192.168.1.121"):
    context = zmq.Context()
    socket = context.socket(zmq.PUB if bind else zmq.SUB)
    if bind:
        socket.bind(f"tcp://*:{port}")
    else:
        socket.connect(f"tcp://{host}:{port}")
        socket.setsockopt(zmq.SUBSCRIBE, b"")
    return socket

def init_push_socket(port):
    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.connect(f"tcp://192.168.1.121:{port}")
    return socket

def init_pull_socket(port):
    context = zmq.Context()
    socket = context.socket(zmq.PULL)
    socket.bind(f"tcp://*:{port}")
    return socket

def send_data(socket, data):
    socket.send(pickle.dumps(data))

def recv_data(socket):
    return pickle.loads(socket.recv())

